

#ifndef _K_MEMORY_ALLOCATER_H_
#define _K_MEMORY_ALLOCATER_H_

#include "MyMemMgr.h"

template<typename T>
class HeapAllocator
{
public:
	// types
	typedef int			ptrdiff_t;	// defined in stddef.h originally.........
	typedef size_t		size_type;
	typedef ptrdiff_t	difference_type;
	typedef T*			pointer;
	typedef const T*	const_pointer;
	typedef T&			reference;
	typedef const T&	const_reference;
	typedef T			value_type;

	inline					HeapAllocator() {}

	// return the address of x, in the form that pointers must take 
	// for allocated elements.
	inline pointer			address(reference x) const;

	// return the address of x, in the form that pointers must take 
	// for allocated elements.
	inline const_pointer	address(const_reference x) const;

	// allocates storage for an array of n elements of type
	inline pointer			allocate(size_t n, const pointer v=0);

	// frees storage for the array of n objects of type beginning at p
	inline void				deallocate(pointer P, size_t s=0);

	// constructs an object of type
	inline void				construct(pointer P, const T& V);

	// destructs an object of type
	inline void				destroy(pointer P);

	// returns the length of the longest sequence of elements of type 
	// that an allocator might be able to allocate.
	inline size_t			max_size() const;

	// returns the memory allocation block size
	inline size_t			block_size() const;

	// re-allocates storage for an array of n elements of type
	inline pointer			reallocate(pointer P, size_t n);

	// special allocator kludge function in place of template function rebind
	inline char*			_Charalloc(size_type n);

protected:

};



//------------------------------------------------------------------------------
// return the address of x, in the form that pointers must take 
// for allocated elements.
//------------------------------------------------------------------------------
template <typename T> inline
HeapAllocator<T>::pointer HeapAllocator<T>::address( reference x ) const	
{ return ( &x ); }

//------------------------------------------------------------------------------
// return the address of x, in the form that pointers must take 
// for allocated elements.
//------------------------------------------------------------------------------
template <typename T> inline
HeapAllocator<T>::const_pointer	HeapAllocator<T>::address( const_reference x ) const
{ return (&x); }

//------------------------------------------------------------------------------
// allocates storage for an array of n elements of type
//------------------------------------------------------------------------------
template <typename T> inline
HeapAllocator<T>::pointer HeapAllocator<T>::allocate( size_t n, const pointer v )
{ return (pointer)MemAlloc( n * sizeof(T) ); }

//------------------------------------------------------------------------------
// frees storage for the array of n objects of type beginning at p
//------------------------------------------------------------------------------
template <typename T> inline
void HeapAllocator<T>::deallocate( pointer P, size_t s )
{ MemFree( P ); }

//------------------------------------------------------------------------------
// constructs an object of type
//------------------------------------------------------------------------------
template <typename T> inline
void HeapAllocator<T>::construct( pointer P, const T& V )
{ new ((void *)P) T(V); }

//------------------------------------------------------------------------------
// destructs an object of type
//------------------------------------------------------------------------------
template <typename T> inline
void HeapAllocator<T>::destroy( pointer P )
{ P->T::~T(); }

//------------------------------------------------------------------------------
// returns the length of the longest sequence of elements of type 
// that an allocator might be able to allocate.
//------------------------------------------------------------------------------
template <typename T> inline
size_t HeapAllocator<T>::max_size() const
{
	size_t n = (size_t)(-1) / sizeof(T);
	return ( 0 < n ? n : 1 );
}

template <typename T> inline
size_t	HeapAllocator<T>::block_size() const
{
	return sizeof(T);
}

//------------------------------------------------------------------------------
// re-allocates storage for an array of n elements of type
//------------------------------------------------------------------------------
template <typename T> inline
HeapAllocator<T>::pointer HeapAllocator<T>::reallocate( pointer P, size_t n )
{ return (pointer)MemReAlloc( P, n * sizeof(T) ); }

//------------------------------------------------------------------------------
// special allocator kludge function in place of template function rebind
//------------------------------------------------------------------------------
template <typename T> inline
char* HeapAllocator<T>::_Charalloc( size_type n )
{ return (char*)MemAlloc( n ); }


//------------------------------------------------------------------------------
//	operator ==() and !=() for allocator template
//------------------------------------------------------------------------------
template<class T, class U> inline
bool operator==( const HeapAllocator<T>& t, const HeapAllocator<U>& u )
{ return true; }

template<class T, class U> inline
bool operator!=( const HeapAllocator<T>&, const HeapAllocator<U>& )
{ return false; }


/*
//------------------------------------------------------------------------------
//	safeDelete()
//		- delete only if non-null pointer
//		- sets pointer to null
//------------------------------------------------------------------------------
template <typename T> 
inline void safeDelete( T& a )
{ 
	if (a)
	{
		delete a;
		a = 0;
	}
}

//------------------------------------------------------------------------------
//	safeRelease()
//		- release() only if non-null pointer
//		- sets pointer to null
//		- returns current ref count
//------------------------------------------------------------------------------
template <typename T> 
inline uint32 safeRelease( T& a )
{ 
	uint32 n=0;
	if (a)
	{
		n = a->Release();
		a = 0;
	}
	return n;
}
*/
#endif